package com.qianchang.microfinance.view.mine.renewal;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.ToastUtil;
import com.dm.library.widgets.indexlib.IndexBar.widget.DividerItemDecoration;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.LoanProductBean;
import com.qianchang.microfinance.bean.PageBean;
import com.qianchang.microfinance.global.ConstantAppInfo;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseActivity;
import com.qianchang.microfinance.view.base.BaseRecyclerAdapter;
import com.qianchang.microfinance.view.base.EmptyLayout;
import com.qianchang.microfinance.view.base.RecyclerRefreshLayout;
import com.qianchang.microfinance.view.mine.RechargeFixActivity;
import com.qianchang.microfinance.view.mine.loan.MyLoanAdapter;
import com.qianchang.microfinance.view.mine.loan.MyLoanDetailActivity;
import com.qianchang.microfinance.widgets.RecyclerViewDivider;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/***
 *  续贷 RenewalAdapter.OnItemClickListener,
 */

public class RenewalActivity extends BaseActivity implements RecyclerRefreshLayout.SuperRefreshLayoutListener,BaseRecyclerAdapter.OnItemClickListener{

    @BindView(R.id.recyclerView)
    RecyclerView recyclerViewRenewal;
    @BindView(R.id.error_layout)
    EmptyLayout errorLayout;
    @BindView(R.id.refreshLayout)
    RecyclerRefreshLayout refreshLayout;

    private RecyclerViewDivider recyclerViewDivider;
    private LinearLayoutManager linearLayoutManager;
    private RenewalAdapter adapter;
    private List<LoanProductBean> listLoan = new ArrayList<LoanProductBean>();
    private int productType = 0;
    protected boolean isRefreshing = false;
    protected int pageIndex = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_renewal_layout);
        ButterKnife.bind(this);
        initView();
        initData();

    }


    @Override
    protected void initView() {
        super.initView();
        setTitleName("续贷");

        initRecyclerView();

        errorLayout.findViewById(R.id.animProgress).setVisibility(View.GONE);
        errorLayout.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                onRefreshing();
            }
        });

    }


    private void initRecyclerView(){
        linearLayoutManager = new LinearLayoutManager(RenewalActivity.this);
        recyclerViewRenewal.setLayoutManager(linearLayoutManager);
        adapter = new RenewalAdapter(RenewalActivity.this);
        adapter.setState(BaseRecyclerAdapter.STATE_HIDE, false);
        recyclerViewRenewal.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
//        recyclerViewDivider = new RecyclerViewDivider(RenewalActivity.this, DividerItemDecoration.VERTICAL_LIST);
//        recyclerViewRenewal.addItemDecoration(recyclerViewDivider);
        recyclerViewRenewal.setHasFixedSize(true);
        recyclerViewRenewal.setNestedScrollingEnabled(false);
        refreshLayout.setSuperRefreshLayoutListener(this);


    }



    @Override
    protected void initData() {
        super.initData();
        getRenewalData();

    }


    private void getRenewalData(){
        adapter.setState(BaseRecyclerAdapter.STATE_HIDE, true);
        HttpParams params = new HttpParams();
        params.put("reqPageNum", 1);
        getData(params);

    }


    private void getData(HttpParams params){

        String urlType = "cash";
        int productType = 0;

        params.put("opSource", 1);
        params.put("maxResults", 10);
        params.put("productType", productType);
        DMOKHttpUtils.getInstance().post(getApplicationContext(), DMConstant.Config.BASE_USER + "/loan/" + urlType + DMConstant.Config.V + "/list.do", params, new RequestCallBack<PageBean<LoanProductBean>>() {

            @Override
            public void onSuccess(JsonResult<PageBean<LoanProductBean>> response) {
                refreshLayout.onComplete();
                refreshLayout.setRefreshing(false);
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    listLoan = response.getObject().getList();
                    setListData(response.getObject());

                } else {
                    isShowEmptyView();
                    ToastUtil.getInstant().show(RenewalActivity.this, response.getDescription());
                }
                setRefreshing(false);
                isShowEmptyView();
            }

            @Override
            public void onFailure(String msg) {
                refreshLayout.onComplete();
                refreshLayout.setRefreshing(false);
                setRefreshing(false);
                isShowErrorView();
                ToastUtil.getInstant().show(RenewalActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {
                refreshLayout.onComplete();
                isShowErrorView();
                ToastUtil.getInstant().show(RenewalActivity.this, msg);
            }
        });

    }

    private void setListData(PageBean<LoanProductBean> pageBean){
        if(pageBean == null || pageBean.equals("")){
            isShowEmptyView();
            return;
        }
        boolean hasMoreData = pageBean.getList() != null && pageBean.isHasNextPage();
        if (isRefreshing) {
            adapter.clear();
            adapter.addAll(pageBean.getList());
            isRefreshing = false;
        } else {
            adapter.addAll(pageBean.getList());
        }
        refreshLayout.setCanLoadMore(hasMoreData);
        pageIndex = isRefreshing ? 1 : pageBean.getPageIndex() + 1;
        if (!hasMoreData) {
            adapter.setState(BaseRecyclerAdapter.STATE_NO_MORE, true);
        }

    }

    public void setRefreshing(boolean refreshing) {
        isRefreshing = refreshing;
    }


    public void isShowEmptyView() {
        if (adapter.getItems().size() > 0) {
            errorLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
            recyclerViewRenewal.setVisibility(View.VISIBLE);
            errorLayout.setVisibility(View.GONE);
        } else {
            recyclerViewRenewal.setVisibility(View.GONE);
            errorLayout.setVisibility(View.VISIBLE);
            errorLayout.setErrorType(isNeedEmptyView() ? EmptyLayout.NO_DATA : EmptyLayout.HIDE_LAYOUT);
        }
    }


    public void isShowErrorView() {
        if (adapter.getItems().size() > 0) {
            errorLayout.setErrorType(EmptyLayout.HIDE_LAYOUT);
            recyclerViewRenewal.setVisibility(View.VISIBLE);
            errorLayout.setVisibility(View.GONE);
        } else {
            recyclerViewRenewal.setVisibility(View.GONE);
            errorLayout.setVisibility(View.VISIBLE);
            errorLayout.setErrorType(isNeedEmptyView() ? EmptyLayout.NETWORK_ERROR : EmptyLayout.HIDE_LAYOUT);
        }
    }

    protected boolean isNeedEmptyView() {
        return true;
    }


    //下来刷新
    @Override
    public void onRefreshing() {
        isRefreshing = true;
        adapter.setState(BaseRecyclerAdapter.STATE_HIDE, true);
        HttpParams params = new HttpParams();
        params.put("reqPageNum", 1);
        getData(params);

    }

    //上拉加载
    @Override
    public void onLoadMore() {
        adapter.setState(isRefreshing ? BaseRecyclerAdapter.STATE_HIDE : BaseRecyclerAdapter.STATE_LOADING, true);
        HttpParams params = new HttpParams();
        params.put("reqPageNum", pageIndex);
        getData(params);
    }


    @Override
    public void onItemClick(int position, long itemId) {
        Bundle bundle = new Bundle();
        LoanProductBean loanProductBean = listLoan.get(position);
        bundle.putString(ConstantAppInfo.PROJECT_ID, loanProductBean.getProjectId());
        bundle.putString(ConstantAppInfo.PROJECT_UNIT, loanProductBean.getUnit());
        bundle.putString(ConstantAppInfo.PROJECT_STATUS, loanProductBean.getStatus());
        bundle.putString(ConstantAppInfo.PROJECT_STATUS_DES, loanProductBean.getStatusDesc());
        bundle.putInt(ConstantAppInfo.PROJECT_TYPE, productType);
        startActivity(MyLoanDetailActivity.class, bundle);
    }
}
